<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record, index, text }">
        <template v-if="column.dataIndex === 'characteristicsFileList'">
          <TableImg :imgList="text" />
        </template>
        <template v-if="column.dataIndex === 'detailsFileList'">
          <TableImg :imgList="text" />
        </template>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <add-drawer v-model:visible="addDrawerVisible" v-if="addDrawerVisible" :isDetail="isDetail" :id="currentId" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="yy_p-yyProductSeries" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable, TableAction, TableImg } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import addDrawer from './add.vue';
  import { columns, searchFormSchema, superQuerySchema } from './data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './api';
  const queryParam = reactive<any>({});
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: 'yy_product_series',
      api: list,
      columns,
      canResize: false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: 'yy_product_series',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload }, { selectedRowKeys }] = tableContext;

  const addDrawerVisible = ref(false);
  const currentId = ref();
  const isDetail = ref(false);

  /**
   * 新增事件
   */
  function handleAdd() {
    isDetail.value = false;
    currentId.value = '';
    addDrawerVisible.value = true;
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    isDetail.value = false;
    currentId.value = record.id;
    addDrawerVisible.value = true;
  }
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    currentId.value = record.id;
    isDetail.value = true;
    addDrawerVisible.value = true;
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
    ];
  }
</script>

<style lang="less" scoped>
  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
</style>
